package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.mtkj.ConfigAddDto;
import com.dog.basics.bean.dto.admin.mtkj.ConfigEditDto;
import com.dog.basics.bean.model.mtkj.MtDeviceConfig;
import com.dog.basics.bean.vo.admin.mtkj.ConfigVo;
import com.dog.basics.dao.mtkj.MtDeviceConfigMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtDeviceConfigService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 设备管理配置信息 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-09-29
 */
@Service
@AllArgsConstructor
public class MtDeviceConfigServiceImpl extends ServiceImpl<MtDeviceConfigMapper, MtDeviceConfig> implements IMtDeviceConfigService {


    /**
     * @Description: 分页设备管理配置信息
     * @Param: status
     * @return: IPage<ConfigVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public ConfigVo getPageList() {
        ConfigVo configVo = new ConfigVo();
        List<MtDeviceConfig> list = this.list(Wrappers.<MtDeviceConfig>lambdaQuery().orderByDesc(MtDeviceConfig::getCreateTime));
        if (CollUtil.isNotEmpty(list)) {
            BeanUtil.copyProperties(list.get(0), configVo);
        }
        return configVo;
    }

    /**
     * @Description: 增加设备管理配置信息
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(ConfigAddDto obj) {
        //判断是否已经存在
        List<MtDeviceConfig> list = this.list(Wrappers.<MtDeviceConfig>lambdaQuery());
        Validator.validateFalse(CollUtil.isNotEmpty(list), "只能有一条数据!");
        MtDeviceConfig dogNews = new MtDeviceConfig();
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setCreateTime(DateUtil.currentSeconds());
        dogNews.setCreateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.save(dogNews), "数据保存失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 修改设备管理配置信息
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(ConfigEditDto obj) {
        MtDeviceConfig dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "无此数据!");
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.updateById(dogNews), "修改数据失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除设备管理配置信息
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        MtDeviceConfig dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "无此数据!");
        Validator.validateTrue(this.removeById(id), "数据删除失败!");
        return Boolean.TRUE;
    }
}
